﻿@{
    ViewBag.Title = "模块维护";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">模块结构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;"><i class="fa fa-minus"></i></a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">菜单列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input  type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default"><i class="fa fa-search"></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
@Html.Partial("_PartialSystemPrivilegeDetail")
<script type="text/javascript">
    var zNodes,
        treeObj,
        $grid,
        height;
    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            shrinkToFit: true,
            multiselect: false,
            url: "/System/Menu/GetMeunuByPId",
            colModel: [
                { name: "MenuId", index: "MenuId", hidden: true },
                //{ label: "代码", name: "Code", width: 140, fixed: true },
                { label: "名称", name: "Name", width: 100, fixed: true },
                { label: "图标", name: "Icon", width: 40, align: "center", fixed: true, formatter: "icon", search: false },
                { label: "地址", name: "OpenUrl", width: 100, fixed: true },
                {
                    label: "可删除",
                    name: "CanbeDelete",
                    width: 50,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                {
                    label: "模块权限",
                    name: "HaveMenuPermission",
                    width: 65,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                {
                    label: "按钮权限",
                    name: "HaveFunctionPermission",
                    width: 65,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                {
                    label: "数据权限",
                    name: "HaveDataPermission",
                    width: 65,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                {
                    label: "字段权限",
                    name: "HaveFieldPermission",
                    width: 65,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },

                {
                    label: "冻结",
                    name: "IsFreeze",
                    width: 40,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                {
                    label: "模块显示",
                    name: "IsShowMenu",
                    width: 65,
                    align: "center",
                    fixed: true,
                    formatter: "YesOrNo"
                },
                { label: "排序号", align: "center", name: "OrderNo", width: 50, fixed: true, search: false },
                { label: "备注", name: "Remark", width: 150, fixed: true }
            ],
            height: height - 100,
            postData: { pId: Language.common.guidempty },
            rowList: [200, 500, 1000],
            subGrid: true,
            subGridRowExpanded: function (subgridId, rowId) {
                var rowDatas = $grid.jqGrid('getRowData', rowId);
                if (typeof (rowDatas["MenuId"]) != "undefined") {
                    getById(Language.privilegeAccess.menu, rowDatas["MenuId"]);
                    //获取html值
                    $("#" + subgridId).html($("#permissionUser").html());
                }
            }
        });
    }

    function getById(access, id) {
        if (id != null && id != "") {
            UtilAjaxPost("/Common/Global/GetSystemPrivilegeDetailOutputsByAccessAndValue", { id: id, access: access },
                function (val) {
                    //角色
                    var roleHtml = template('role-template', val);
                    $("#role").html(roleHtml);

                    //岗位
                    var postHtml = template('post-template', val);
                    $("#post").html(postHtml);

                    //组
                    var groupHtml = template('group-template', val);
                    $("#group").html(groupHtml);

                    //组织机构
                    var organizationHtml = template('organization-template', val);
                    $("#organization").html(organizationHtml);

                    //人员
                    var userHtml = template('user-template', val);
                    $("#user").html(userHtml);
                }
            );
        } else {

            var val = [];
            //角色
            var roleHtml = template('role-template', val);
            $("#role").html(roleHtml);

            //岗位
            var postHtml = template('post-template', val);
            $("#post").html(postHtml);

            //组
            var groupHtml = template('group-template', val);
            $("#group").html(groupHtml);

            //组织机构
            var organizationHtml = template('organization-template', val);
            $("#organization").html(organizationHtml);

            //人员
            var userHtml = template('user-template', val);
            $("#user").html(userHtml);
        }
    }

    //获取表格数据
    function getGridData() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var pId;
        if (treeNode.length === 0) {
            pId = Language.common.guidempty;
        } else {
            pId = treeNode[0].id;
        }
        UtilAjaxPost("/System/Menu/GetMeunuByPId", { id: pId }, function (data) {
            GridReloadLoadOnceData($grid, data);
        });
    }

    //初始化模块
    function initTree() {
        //配置
        var setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClickTree
            }
        };
        treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    }

    //初始化树结构
    function initTreeData() {
        UtilAjaxPost("/System/Menu/GetAllMenu", null, function (data) {
            zNodes = data;
            initTree();
        });
    }

    //树点击触发
    function onClickTree(e, treeId, treeNode) {
        $("#MenuId").val(treeNode.id);
        //加载数据
        getGridData();
    }

    //操作:新增
    function add() {
        var pid = "";
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        if (treeNode.length !== 0) {
            pid = treeNode[0].id;
        }
        ArtDialogOpen("/System/Menu/Edit?parentId=" + pid, "新增模块", 640);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Menu/Edit?id=" + info.MenuId, "修改模块-" + info.Name, 640);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function () {
            ArtDialogConfirm("<lable style='color:red;font-size:14px'>删除前请确认该菜单权限使用者已取消该菜单?</lable><br/>删除后不可恢复,确定删除?", function () {
                UtilAjaxPostWait(
                    "/System/Menu/DeleteMenu",
                    { id: GridGetSingSelectData($grid).MenuId },
                    perateStatus
                );
            });
        });
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            var breforeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
            initTreeData();
            //重新获取该选中树
            if (breforeNode.length !== 0) {
                //获取选中前节点
                var note = treeObj.getNodeByParam("id", breforeNode[0].id, null);
                //获取选中前节点
                treeObj.selectNode(note);
                treeObj.expandNode(note);
            }

            getGridData();
        }
    }

    var menuObj;
    //请求完成后使用
    function loadTreeAndGrid() {
        //重新加载数数据
        initTreeData();
        //获取选中前节点
        var note = treeObj.getNodeByParam("id", menuObj.pId, null);
        //选中加载前节点
        treeObj.selectNode(note);
        treeObj.expandNode(note, true, false);
        //加载列表数据
        getGridData();
    }

    //批量生成代码
    function generatingCode() {
        ArtDialogConfirm("是否批量生成代码?生成的代码将覆盖以前代码,使用模块代码处可能有误", function () {
            UtilAjaxPostWait(
                "/System/Menu/GeneratingCode", {},
                perateStatus
            );
        });
    }

    $(function () {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initTreeData();
        initGird();
        $(".icon-picker").iconPicker();
    });

    $(window).bind("resize", function () {
        var width = $(".col-md-9").width();
        $grid.setGridWidth(width - 12);
    });
</script>